package com.lwb.ztx.lives.model;

import android.content.Context;
import android.os.AsyncTask;

import com.lwb.ztx.bean.LiveBean;
import com.lwb.ztx.utils.LogUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by luweibin on 2016/6/13.
 */
public class LiveModelImpl implements LiveModel {

    OnReadLivesFinishListener listener;

    private List<ChannelObj> channels = new ArrayList();

    public class ChannelObj {
        int id;
        String name;

        public ChannelObj(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public LiveModelImpl(OnReadLivesFinishListener listener) {
        this.listener = listener;
    }

    @Override
    public void readLiveFile(final Context context) {
        AsyncTask asyncTask = new AsyncTask<Object, Void, List<LiveBean>>() {
            @Override
            protected List<LiveBean> doInBackground(Object[] params) {
                List<LiveBean> beans = new ArrayList<>();
                try {
//                    InputStream is = context.getAssets().open("channels_0714.txt");
//                    InputStreamReader read = new InputStreamReader(is, "UTF-8");
//                    BufferedReader bufferedReader = new BufferedReader(read);
//                    String line = null;
//                    while ((line = bufferedReader.readLine()) != null) {
//                        if (line.trim().equals("")) {
//                            continue;
//                        }
//                        LogUtils.i(line);
//                        String[] item = line.split(",");
//                        LiveBean bean = new LiveBean();
//                        if (item.length > 0) {
//                            bean.setTitle(item[0].trim());
//                            if (item[0].contains("#")) {
//                                continue;
//                            }
//                        }
//                        if (item.length > 1)
//                        bean.setPath(item[1].trim());
//                        beans.add(bean);
//                        LogUtils.i(bean.toString());
//                    }

                    channels.add(new ChannelObj( 1017,"CCTV1"));
                    channels.add(new ChannelObj( 1023,"CCTV2"));
                    channels.add(new ChannelObj( 1026,"CCTV3"));
                    channels.add(new ChannelObj( 1118,"CCTV4"));
                    channels.add(new ChannelObj( 1028,"CCTV5"));
                    channels.add(new ChannelObj( 1184,"CCTV5+"));
                    channels.add(new ChannelObj( 1030,"CCTV6"));
                    channels.add(new ChannelObj( 1120,"CCTV7"));
                    channels.add(new ChannelObj( 1032,"CCTV8"));
                    channels.add(new ChannelObj( 1035,"CCTV9"));
                    channels.add(new ChannelObj( 1124,"CCTV10"));
                    channels.add(new ChannelObj( 1126,"CCTV11"));
                    channels.add(new ChannelObj( 1128,"CCTV12"));
                    channels.add(new ChannelObj( 1130,"CCTV13"));
                    channels.add(new ChannelObj( 1132,"CCTV少儿"));
                    channels.add(new ChannelObj( 1134,"CCTV音乐"));
                    channels.add(new ChannelObj( 1122,"CCTVnews"));
                    channels.add(new ChannelObj( 1136,"欧洲足球"));
                    channels.add(new ChannelObj( 1199,"CCTV风云足球"));
                    channels.add(new ChannelObj( 1201,"CCTV高尔夫网球"));
                    channels.add(new ChannelObj( 1203,"CCTV世界地理"));
                    channels.add(new ChannelObj( 1205,"CCTV国防军事"));
                    channels.add(new ChannelObj( 1207,"CCTV风云剧场"));
                    channels.add(new ChannelObj( 1209,"CCTV央视文化精品"));
                    channels.add(new ChannelObj( 1211,"CCTV怀旧剧场"));
                    channels.add(new ChannelObj( 1213,"CCTV风云音乐"));
                    channels.add(new ChannelObj( 1291,"CCTV女性时尚"));
                    channels.add(new ChannelObj( 1293,"CCTV央视台球"));
                    channels.add(new ChannelObj( 1295,"CCTV第一剧场"));
                    channels.add(new ChannelObj( 1297,"CCTV5+体育赛事"));
                    channels.add(new ChannelObj( 1287,"CCTV新科动漫"));
                    channels.add(new ChannelObj( 1140,"河北卫视"));
                    channels.add(new ChannelObj( 1142,"内蒙古卫视"));
                    channels.add(new ChannelObj( 1143,"宁夏卫视"));
                    channels.add(new ChannelObj( 1168,"广西卫视"));
                    channels.add(new ChannelObj( 1170,"旅游卫视"));
                    channels.add(new ChannelObj( 1172,"重庆卫视"));
                    channels.add(new ChannelObj( 1174,"四川卫视"));
                    channels.add(new ChannelObj( 1176,"河南卫视"));
                    channels.add(new ChannelObj( 1178,"东南卫视"));
                    channels.add(new ChannelObj( 1180,"东方卫视"));
                    channels.add(new ChannelObj( 1182,"江西卫视"));
                    channels.add(new ChannelObj( 1221,"浙江卫视"));
                    channels.add(new ChannelObj( 1223,"东方卫视HD"));
                    channels.add(new ChannelObj( 1225,"北京卫视HD"));
                    channels.add(new ChannelObj( 1247,"安徽卫视HD"));
                    channels.add(new ChannelObj( 1249,"山东卫视HD"));
                    channels.add(new ChannelObj( 1251,"湖北卫视HD"));
                    channels.add(new ChannelObj( 1253,"天津卫视HD"));
                    channels.add(new ChannelObj( 1255,"黑龙江卫视HD"));
                    channels.add(new ChannelObj( 1229,"深圳卫视HD"));
                    channels.add(new ChannelObj( 1233,"广东卫视"));
                    channels.add(new ChannelObj( 1186,"吉林卫视"));
                    channels.add(new ChannelObj( 1190,"山西卫视"));
                    channels.add(new ChannelObj( 1194,"青海卫视"));
                    channels.add(new ChannelObj( 1148,"卡酷"));
                    channels.add(new ChannelObj( 1154,"MTV"));
                    channels.add(new ChannelObj( 1152,"数码时代"));
                    channels.add(new ChannelObj( 1215,"淮安公共"));
                    channels.add(new ChannelObj( 1227,"精品剧场"));
                    channels.add(new ChannelObj( 1231,"少儿动漫"));
                    channels.add(new ChannelObj( 1235,"嘉佳卡通"));
                    channels.add(new ChannelObj( 1237,"欧美影院"));
                    channels.add(new ChannelObj( 1241,"探索记录"));
                    channels.add(new ChannelObj( 1257,"风尚音乐"));
                    channels.add(new ChannelObj( 1300,"影视娱乐"));
                    channels.add(new ChannelObj( 1289,"iPanda"));
                    channels.add(new ChannelObj( 1239,"CCTV5HD"));
                    channels.add(new ChannelObj( 1043,"CCTV5+HD"));
                    channels.add(new ChannelObj( 1037,"CCTV?"));
                    channels.add(new ChannelObj( 1166,"？？？？"));
                    channels.add(new ChannelObj( 1285,"？？？？"));
                    channels.add(new ChannelObj( 1188,"？？？？"));
                    channels.add(new ChannelObj( 1192,"？？？？"));
                    channels.add(new ChannelObj( 1196,"？？？？"));
                    for(int i=0;i<channels.size();i++) {
                        LiveBean bean = new LiveBean();
                        bean.setTitle("" + channels.get(i).getName());
                        bean.setPath("http://222.191.24.5:6610/2/2/ch0000009099000000" + channels.get(i).getId() + "/index.m3u8?ispcode=3");
                        beans.add(bean);
                    }

//                    for(int i=1000;i<1100;i++) {
//                        LiveBean bean = new LiveBean();
//                        bean.setTitle("" + i);
//                        bean.setPath("http://222.191.24.5:6610/2/2/ch0000009099000000" + i + "/index.m3u8?ispcode=3");
//                        beans.add(bean);
//                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
                return beans;
            }

            @Override
            protected void onPostExecute(List<LiveBean> been) {
                LogUtils.i("Load finish: " + been.toString() );
                super.onPostExecute(been);
                if (listener != null) {
                    listener.onReadSuccess(been);
                }
            }
        };
        asyncTask.execute();
    }

    public interface OnReadLivesFinishListener {
        void onReadSuccess(List<LiveBean> beans);
    }
}
